package com.bkgy.jobs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bkgy.modular.datacollect_bx.bean.JDataSavemong1630;
import com.bkgy.modular.tpdodata_cal1630.entity.TBTensionDataCal1630;
import com.bkgy.modular.tpdodata_cal1630.entity.TPdoDataCal1630;
import com.bkgy.modular.tpdodata_cal1630.mapper.TBTensionDataCal1630Mapper;
import com.bkgy.modular.tpdodata_cal1630.service.TPdoDataCal1630Service;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class FurnaceRollerTensionJobs {
    private final TPdoDataCal1630Service cal1630Service;
    private final TBTensionDataCal1630Mapper tbTensionDataCal1630Mapper;
    @Resource
    private MongoTemplate mongoTemplate;

    public void run() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.HOUR_OF_DAY, -12);
        String levelTime = DateUtil.getDateToString(cal.getTime(), "yyyyMMddHHmmss");

        LambdaQueryWrapper<TPdoDataCal1630> w = new LambdaQueryWrapper<>();
        w.select(TPdoDataCal1630::getExtid, TPdoDataCal1630::getEntid, TPdoDataCal1630::getYpClass,
                TPdoDataCal1630::getSteelgrade, TPdoDataCal1630::getExthkClass, TPdoDataCal1630::getExwidthClass,TPdoDataCal1630::getOutMatWidth,TPdoDataCal1630::getOutMatThk);
        w.eq(TPdoDataCal1630::getTensionFlag, 0).ge(TPdoDataCal1630::getProdEndTime, levelTime);
        List<TPdoDataCal1630> list = cal1630Service.list(w);
        for (int i = 0; i < list.size(); i++) {
            TPdoDataCal1630 tPdoDataCal1630 = list.get(i);
            furnaceRollerSimulation(tPdoDataCal1630);
        }
    }

    public void furnaceRollerSimulation(TPdoDataCal1630 tPdoDataCal1630) {
        Query query = new Query();
        query.addCriteria(Criteria.where("extid").is(tPdoDataCal1630.getExtid()));
        String[] arr = new String[]{
                "cal_fcs_ten_avg", "cal_fcs_ten_max", "cal_fcs_ten_min", "cal_heat1_ten_avg", "cal_heat1_ten_max",//
                "cal_heat1_ten_min", "cal_heat2_ten_avg", "cal_heat2_ten_max", "cal_heat2_ten_min", "cal_oas1_ten_avg",//
                "cal_oas1_ten_max", "cal_oas1_ten_min", "cal_oas2_ten_avg", "cal_oas2_ten_max", "cal_oas2_ten_min",//
                "cal_oas3_ten_avg", "cal_oas3_ten_max", "cal_oas3_ten_min", "cal_preheat_ten_avg", "cal_preheat_ten_max",//
                "cal_preheat_ten_min", "cal_rcs_ten_avg", "cal_rcs_ten_max", "cal_rcs_ten_min", "cal_scs_ten_avg",//
                "cal_scs_ten_max", "cal_scs_ten_min", "cal_soak_ten_avg", "cal_soak_ten_max", "cal_soak_ten_min",//
                "cal_clean_ten_avg", "cal_clean_ten_max", "cal_clean_ten_min", "cal_enlooper_ten_act_avg", "cal_enlooper_ten_act_max",//
                "cal_enlooper_ten_act_min", "cal_midlooper_ten_act_avg", "cal_midlooper_ten_act_max", "cal_midlooper_ten_act_min", "cal_exlooper_ten_act_avg",//
                "cal_exlooper_ten_act_max", "cal_exlooper_ten_act_min", "cal_por1_ten_avg", "cal_por1_ten_max", "cal_por1_ten_min",//
                "cal_por2_ten_avg", "cal_por2_ten_max", "cal_por2_ten_min", "cal_enspm_ten_act_avg", "cal_enspm_ten_act_max",//
                "cal_enspm_ten_act_min", "cal_exspm_ten_act_avg", "cal_exspm_ten_act_max", "cal_exspm_ten_act_min"
        };
        for (int i = 0; i < arr.length; i++) {
            query.fields().include("trailValues." + arr[i]);
        }
        JDataSavemong1630 mongoData = mongoTemplate.findOne(query, JDataSavemong1630.class);
        if (mongoData != null) {
            try {
                TBTensionDataCal1630 tbTensionDataCal1630 = new TBTensionDataCal1630();
                Map<String, Double> trailValues = mongoData.getTrailValues();
                if (trailValues.containsKey(arr[0]))
                    tbTensionDataCal1630.setFinalTensionAve(BigDecimal.valueOf(trailValues.get(arr[0])));
                if (trailValues.containsKey(arr[1]))
                    tbTensionDataCal1630.setFinalTensionMax(BigDecimal.valueOf(trailValues.get(arr[1])));
                if (trailValues.containsKey(arr[2]))
                    tbTensionDataCal1630.setFinalTensionMin(BigDecimal.valueOf(trailValues.get(arr[2])));
                if (trailValues.containsKey(arr[3]))
                    tbTensionDataCal1630.setHeat1TensionAve(BigDecimal.valueOf(trailValues.get(arr[3])));
                if (trailValues.containsKey(arr[4]))
                    tbTensionDataCal1630.setHeat1TensionMax(BigDecimal.valueOf(trailValues.get(arr[4])));
                if (trailValues.containsKey(arr[5]))
                    tbTensionDataCal1630.setHeat1TensionMin(BigDecimal.valueOf(trailValues.get(arr[5])));
                if (trailValues.containsKey(arr[6]))
                    tbTensionDataCal1630.setHeat2TensionAve(BigDecimal.valueOf(trailValues.get(arr[6])));
                if (trailValues.containsKey(arr[7]))
                    tbTensionDataCal1630.setHeat2TensionMax(BigDecimal.valueOf(trailValues.get(arr[7])));
                if (trailValues.containsKey(arr[8]))
                    tbTensionDataCal1630.setHeat2TensionMin(BigDecimal.valueOf(trailValues.get(arr[8])));
                if (trailValues.containsKey(arr[9]))
                    tbTensionDataCal1630.setStage1TensionAve(BigDecimal.valueOf(trailValues.get(arr[9])));

                if (trailValues.containsKey(arr[10]))
                    tbTensionDataCal1630.setStage1TensionMax(BigDecimal.valueOf(trailValues.get(arr[10])));
                if (trailValues.containsKey(arr[11]))
                    tbTensionDataCal1630.setStage1TensionMin(BigDecimal.valueOf(trailValues.get(arr[11])));
                if (trailValues.containsKey(arr[12]))
                    tbTensionDataCal1630.setStage2TensionAve(BigDecimal.valueOf(trailValues.get(arr[12])));
                if (trailValues.containsKey(arr[13]))
                    tbTensionDataCal1630.setStage2TensionMax(BigDecimal.valueOf(trailValues.get(arr[13])));
                if (trailValues.containsKey(arr[14]))
                    tbTensionDataCal1630.setStage2TensionMin(BigDecimal.valueOf(trailValues.get(arr[14])));
                if (trailValues.containsKey(arr[15]))
                    tbTensionDataCal1630.setStage3TensionAve(BigDecimal.valueOf(trailValues.get(arr[15])));
                if (trailValues.containsKey(arr[16]))
                    tbTensionDataCal1630.setStage3TensionMax(BigDecimal.valueOf(trailValues.get(arr[16])));
                if (trailValues.containsKey(arr[17]))
                    tbTensionDataCal1630.setStage3TensionMin(BigDecimal.valueOf(trailValues.get(arr[17])));
                if (trailValues.containsKey(arr[18]))
                    tbTensionDataCal1630.setPreheatTensionAve(BigDecimal.valueOf(trailValues.get(arr[18])));
                if (trailValues.containsKey(arr[19]))
                    tbTensionDataCal1630.setPreheatTensionMax(BigDecimal.valueOf(trailValues.get(arr[19])));

                if (trailValues.containsKey(arr[20]))
                    tbTensionDataCal1630.setPreheatTensionMin(BigDecimal.valueOf(trailValues.get(arr[20])));
                if (trailValues.containsKey(arr[21]))
                    tbTensionDataCal1630.setRcsTensionAve(BigDecimal.valueOf(trailValues.get(arr[21])));
                if (trailValues.containsKey(arr[22]))
                    tbTensionDataCal1630.setRcsTensionMax(BigDecimal.valueOf(trailValues.get(arr[22])));
                if (trailValues.containsKey(arr[23]))
                    tbTensionDataCal1630.setRcsTensionMin(BigDecimal.valueOf(trailValues.get(arr[23])));
                if (trailValues.containsKey(arr[24]))
                    tbTensionDataCal1630.setSlowTensionAve(BigDecimal.valueOf(trailValues.get(arr[24])));
                if (trailValues.containsKey(arr[25]))
                    tbTensionDataCal1630.setSlowTensionMax(BigDecimal.valueOf(trailValues.get(arr[25])));
                if (trailValues.containsKey(arr[26]))
                    tbTensionDataCal1630.setSlowTensionMin(BigDecimal.valueOf(trailValues.get(arr[26])));
                if (trailValues.containsKey(arr[27]))
                    tbTensionDataCal1630.setSoakTensionAve(BigDecimal.valueOf(trailValues.get(arr[27])));
                if (trailValues.containsKey(arr[28]))
                    tbTensionDataCal1630.setSoakTensionMax(BigDecimal.valueOf(trailValues.get(arr[28])));
                if (trailValues.containsKey(arr[29]))
                    tbTensionDataCal1630.setSoakTensionMin(BigDecimal.valueOf(trailValues.get(arr[29])));

                if (trailValues.containsKey(arr[30]))
                    tbTensionDataCal1630.setCleanTensionAve(BigDecimal.valueOf(trailValues.get(arr[30])));
                if (trailValues.containsKey(arr[31]))
                    tbTensionDataCal1630.setCleanTensionMax(BigDecimal.valueOf(trailValues.get(arr[31])));
                if (trailValues.containsKey(arr[32]))
                    tbTensionDataCal1630.setCleanTensionMin(BigDecimal.valueOf(trailValues.get(arr[32])));
                if (trailValues.containsKey(arr[33]))
                    tbTensionDataCal1630.setEnlooperTensionAve(BigDecimal.valueOf(trailValues.get(arr[33])));
                if (trailValues.containsKey(arr[34]))
                    tbTensionDataCal1630.setEnlooperTensionMax(BigDecimal.valueOf(trailValues.get(arr[34])));
                if (trailValues.containsKey(arr[35]))
                    tbTensionDataCal1630.setEnlooperTensionMin(BigDecimal.valueOf(trailValues.get(arr[35])));
                if (trailValues.containsKey(arr[36]))
                    tbTensionDataCal1630.setMidlooperTensionAve(BigDecimal.valueOf(trailValues.get(arr[36])));
                if (trailValues.containsKey(arr[37]))
                    tbTensionDataCal1630.setMidlooperTensionMax(BigDecimal.valueOf(trailValues.get(arr[37])));
                if (trailValues.containsKey(arr[38]))
                    tbTensionDataCal1630.setMidlooperTensionMin(BigDecimal.valueOf(trailValues.get(arr[38])));
                if (trailValues.containsKey(arr[39]))
                    tbTensionDataCal1630.setExlooperTensionAve(BigDecimal.valueOf(trailValues.get(arr[39])));

                if (trailValues.containsKey(arr[40]))
                    tbTensionDataCal1630.setExlooperTensionMax(BigDecimal.valueOf(trailValues.get(arr[40])));
                if (trailValues.containsKey(arr[41]))
                    tbTensionDataCal1630.setExlooperTensionMin(BigDecimal.valueOf(trailValues.get(arr[41])));
                if (trailValues.containsKey(arr[42]))
                    tbTensionDataCal1630.setPort1TensionAve(BigDecimal.valueOf(trailValues.get(arr[42])));
                if (trailValues.containsKey(arr[43]))
                    tbTensionDataCal1630.setPort1TensionMax(BigDecimal.valueOf(trailValues.get(arr[43])));
                if (trailValues.containsKey(arr[44]))
                    tbTensionDataCal1630.setPort1TensionMin(BigDecimal.valueOf(trailValues.get(arr[44])));
                if (trailValues.containsKey(arr[45]))
                    tbTensionDataCal1630.setPort2TensionAve(BigDecimal.valueOf(trailValues.get(arr[45])));
                if (trailValues.containsKey(arr[46]))
                    tbTensionDataCal1630.setPort2TensionMax(BigDecimal.valueOf(trailValues.get(arr[46])));
                if (trailValues.containsKey(arr[47]))
                    tbTensionDataCal1630.setPort2TensionMin(BigDecimal.valueOf(trailValues.get(arr[47])));
                if (trailValues.containsKey(arr[48]))
                    tbTensionDataCal1630.setEnspmTensionAve(BigDecimal.valueOf(trailValues.get(arr[48])));
                if (trailValues.containsKey(arr[49]))
                    tbTensionDataCal1630.setEnspmTensionMax(BigDecimal.valueOf(trailValues.get(arr[49])));

                if (trailValues.containsKey(arr[50]))
                    tbTensionDataCal1630.setEnspmTensionMin(BigDecimal.valueOf(trailValues.get(arr[50])));
                if (trailValues.containsKey(arr[51]))
                    tbTensionDataCal1630.setExspmTensionAve(BigDecimal.valueOf(trailValues.get(arr[51])));
                if (trailValues.containsKey(arr[52]))
                    tbTensionDataCal1630.setExspmTensionMax(BigDecimal.valueOf(trailValues.get(arr[52])));
                if (trailValues.containsKey(arr[53]))
                    tbTensionDataCal1630.setExspmTensionMin(BigDecimal.valueOf(trailValues.get(arr[53])));
                tbTensionDataCal1630.setEntid(tPdoDataCal1630.getEntid());
                tbTensionDataCal1630.setExtid(tPdoDataCal1630.getExtid());
                tbTensionDataCal1630.setYpClass(tPdoDataCal1630.getYpClass());
                tbTensionDataCal1630.setSteelGrade(tPdoDataCal1630.getSteelgrade());
                tbTensionDataCal1630.setExthkClass(tPdoDataCal1630.getExthkClass());
                tbTensionDataCal1630.setExwidthClass(tPdoDataCal1630.getExwidthClass());
                tbTensionDataCal1630.setOutMatWidth(tPdoDataCal1630.getOutMatWidth());
                tbTensionDataCal1630.setOutMatThk(tPdoDataCal1630.getOutMatThk());

                tbTensionDataCal1630Mapper.insert(tbTensionDataCal1630);
                tPdoDataCal1630.setTensionFlag(1);
                cal1630Service.updateById(tPdoDataCal1630);
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getMessage(), e);
            }
        }
    }
}
